package com.android.plc.ui.adapter;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.plc.R;
import com.android.plc.classholder.BasicItem.Type;
import com.android.plc.classholder.Profile;
import com.android.plc.provider.PLC;
import com.android.plc.util.CursorUtil;

public class ProfileAdapter extends BaseAdapter
{
	private Context mContext = null;
	private Cursor mCursor = null;
	private boolean mEditMode = false;
	private int[] from_idx = null;
	private int[] to_idx = null;
	private int mItemID = -1;

	public String mEditString;
	public String mAllString;

	public ProfileAdapter(Context context, Cursor cursor, int item_id, String[] from, int[] to, boolean edit)
	{
		mContext = context;
		mCursor = cursor;
		mEditMode = edit;
		to_idx = to;
		from_idx = new int[from.length];
		mItemID = item_id;
		for (int i = 0; i < from.length; i++)
		{
			from_idx[i] = mCursor.getColumnIndex(from[i]);
		}
		ContentResolver resolver = context.getContentResolver();
		resolver.registerContentObserver(PLC.Profiles.CONTENT_URI, true, mContentObserver);

		Resources resources = context.getResources();
		mAllString = resources.getString(R.string.control);
		mEditString = resources.getString(R.string.add);
	}

	private ContentObserver mContentObserver = new ContentObserver(new Handler())
	{
		@Override
		public void onChange(boolean selfChange)
		{
			mCursor.requery();
			notifyDataSetChanged();
		}
	};

	public Profile getItem(Type type)
	{
		return new Profile(type);
	}

	public Profile getItem(Cursor cursor, int index)
	{
		Profile profile = CursorUtil.buildProfileFromCursor(cursor, index);
		return profile;
	}

	public int getCount()
	{
		return mEditMode ? 2 + mCursor.getCount() : 1 + mCursor.getCount();
	}

	public Object getItem(int position)
	{
		if (position == 0)
		{
			return getItem(Type.ALL_ITEM);
		} else if (mEditMode && position == 1)
		{
			return getItem(Type.ADD_ITEM);
		} else if (mCursor.moveToPosition(coverPosition(position)))
		{
			return getItem(mCursor, coverPosition(position));
		}
		Log.d("TAG", "EEEEEEEEEEEEEEEE");
		return null;
	}

	private int coverPosition(int pos)
	{
		return pos - (mEditMode ? 2 : 1);
	}

	public long getItemId(int position)
	{
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		View view = convertView;
		if (view == null)
		{
			LayoutInflater inflater = LayoutInflater.from(mContext);
			view = inflater.inflate(mItemID, parent, false);
		}
	
		if (position == 0)
		{
			setTextView(view, to_idx[0], mAllString);
		} else if (mEditMode && position == 1)
		{
			setTextView(view, to_idx[0], mEditString);
		} else if (mCursor.moveToPosition(coverPosition(position)))
		{
			for (int i = 0; i < from_idx.length; i++)
			{
				if (from_idx[i] == -1)
					continue;

				String s = mCursor.getString(from_idx[i]);
				setTextView(view, to_idx[i], s);
			}
		}
	//	view.setTag(getItem(position));
		return view;
	}

	private void setTextView(View parentView, int textViewID, String text)
	{
		if (parentView != null)
		{
			TextView tv = (TextView) parentView.findViewById(textViewID);
			if (tv != null)
			{
				tv.setText(text);
				tv.setBackgroundResource(R.drawable.profile_bgr);
			}
		}
	}

	public void setEditMode(boolean mEditMode)
	{
		this.mEditMode = mEditMode;
		notifyDataSetChanged();
	}

	public boolean isEditMode()
	{
		return mEditMode;
	}
}
